package com.woniuxy.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.comon.RedisConst;
import com.woniuxy.portal.mapper.BookMapper;
import com.woniuxy.portal.model.entity.Book;
import com.woniuxy.portal.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{
    @Autowired
    private BookMapper bookMapper;

    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_LIST, keyGenerator = "keyGenerator")
    public Page<Book> findTopSales(Integer pageNum, Integer pageSize) {
        Page<Book> bookPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Book::getBuyCount);
        Page<Book> page = bookMapper.selectPage(bookPage, wrapper);


        return page;
    }

    @Override
    public Page<Book> findByType(int pageNum, int pageSize, int typeId) {
        //1.创建分页对象
        Page<Book> page = new Page<>(pageNum, pageSize);
        //2.按更新时间降序排序
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Book::getTypeId, typeId);
        wrapper.orderByDesc(Book::getUpdateTime);
        //3.执行查询
        bookMapper.selectPage(page, wrapper);
        //4.返回分页对象
        return page;
    }

    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_DETAIL, key = "#root.methodName + ':' + #id")
    public Book findById(Long id) {
        return bookMapper.selectById(id);
    }
}




